package com.yc.system.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.framework.baseClass.R;
import com.yc.system.dto.OrderDto;
import com.yc.system.dto.UserDto;
import com.yc.system.entity.BookOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 订单交易 Mapper 接口
 * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-06
 */
@Mapper
public interface BookOrderMapper extends BaseMapper<BookOrder> {

    @Select("select * from t_book_order where book_id in (select book_id from t_book where user_id=#{userId})")
    List<BookOrder> getOrderListByIssureId(int userId);

    @Select("select * from t_book_order where user_id=#{userId}")
    List<BookOrder> getOrderListByUserId(int userId);

    @Select("select  MAX(order_num+0) from t_book_order")
    long getMaxOrderNum();

    @Update("update t_book_order set order_status=#{orderStatus} where order_id=#{orderId}")
    int updateOrder(BookOrder bookOrder);

    @Select("select * from t_book_order where user_id=#{userId} and deleted=0")
    List<BookOrder> getBooksByPageRandom(IPage<BookOrder> page, int userId);

//    private String bookName;
//    private String coverPhoto;
//    private String isbn;
//    private String author;
//
//    private int issuerId;
//    private String issuerName;
//    private String address;

    @Select("select orders.*,book.book_name,book.cover_photo,book.isbn,book.author,issuer.user_id issuerId,issuer.username issuerName,issuer.address\n" +
            "from t_book_order as orders join t_book as book join t_user as issuer on orders.book_id=book.book_id and book.user_id=issuer.user_id\n" +
            "where orders.user_id=#{userId} and orders.deleted=0 and orders.order_status!=8")
    List<OrderDto> getOrders(IPage<OrderDto> page, int userId);

    @Select("select count(1) from t_book_order orders where orders.user_id=#{userId} and orders.deleted=0 and orders.order_status!=8")
    int getOrdersTotal(int userId);

    @Select("select orders.*,book.book_name,book.cover_photo,book.isbn,book.author,issuer.user_id issuerId,issuer.username issuerName,issuer.address\n" +
            "from t_book_order as orders join t_book as book join t_user as issuer on orders.book_id=book.book_id and book.user_id=issuer.user_id\n" +
            "where book.user_id=#{userId} and orders.deleted=0 and orders.order_status!=9")
    List<OrderDto> getOrdersToStore(IPage<OrderDto> page, int userId);

    @Select("select count(1) from t_book_order as orders join t_book as book join t_user as issuer on orders.book_id=book.book_id and book.user_id=issuer.user_id\n"  +
            " where book.user_id=#{userId} and orders.deleted=0 and orders.order_status!=9")
    int getOrdersToStoreTotal( int userId);

    @Update("update t_book_order set order_status=#{orderStatus} where order_id=#{orderId}")
    int changeOrderStatus(Integer orderId, Integer orderStatus);

    @Select("select t_user.* from t_user join t_book on t_user.user_id=t_book.user_id join t_book_order on t_book.book_id=t_book_order.book_id where t_book_order.order_id=#{orderId}")
    UserDto getissuerInfoByOrderId(int orderId);
    @Select("select t_user.* from t_user join t_book_order on t_user.user_id=t_book_order.user_id where t_book_order.order_id=#{orderId}")
    UserDto getCustomerInfoByOrderId(int orderId);

}
